<?php

namespace App\Http\Controllers\Admin;

use App\Http\Servers\Admin\BoxServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BoxController extends CommonController
{
    //内容盒子列表
    public function box(Request $request, BoxServer $boxServer)
    {
        if ($request->has('status')) {
            $box = \App\Box::findorfail($request->status);

            if ($message['message'] = $boxServer->noPower()) {
                return back()->with($message);
            }

            $box->status = (bool)$box->status ? 0 : 1;
            $result = $box->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', '内容盒子 ' . $box->name . ' 状态修改成功!');
            } else {
                $message['message'] = message(false, '修改失败', '内容盒子 ' . $box->name . ' 状态修改失败!');
            }
            return back()->with($message);
        }

        if ($request->has('del')) {
            $box = \App\Box::findorfail($request->del);

            if ($message['message'] = $boxServer->noPower()) {
                return back()->with($message);
            }

            $result = $box->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', '内容盒子 ' . $box->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', '内容盒子 ' . $box->name . ' 删除失败!');
            }
            return back()->with($message);
        }

        $data['boxes'] = \App\Box::join('admins', 'admins.id', '=', 'boxes.author')
            ->select('boxes.*', 'admins.name as author')
            ->get();

        return view('admin.box.box', $data);
    }

    //内容盒子添加
    public function box_add(Request $request,BoxServer $boxServer)
    {
        if ($request->isMethod('post')) {
            if ($message['message'] = $boxServer->noPower()) {
                return back()->with($message);
            }

            $this->validate($request, [
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:boxes',
                'describe' => 'required',
            ]);

            $create['name'] = $request->name;
            $create['path'] = $request->path;
            $create['describe'] = $request->describe;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['status'] = $request->has('status') ? 1 : 0;

            $result = \App\Box::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', '内容盒子 ' . $request->name . ' 添加成功!');
            } else {
                $message['message'] = message(false, '添加失败', '内容盒子 ' . $request->name . ' 添加失败!');
            }
            return back()->with($message);
        }

        return view('admin.box.box_add');
    }

    //内容盒子编辑
    public function box_edit(Request $request,BoxServer $boxServer)
    {
        if ($request->isMethod('post')) {
            if ($message['message'] = $boxServer->noPower()) {
                return back()->with($message);
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:boxes,path,' . $request->id . ',id',
                'describe' => 'required',
            ]);

            $update['name'] = $request->name;
            $update['path'] = $request->path;
            $update['describe'] = $request->describe;
            $update['author'] = \Auth::guard('admin')->user()->id;
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\Box::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', '内容盒子 ' . $request->name . ' 编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '内容盒子 ' . $request->name . ' 编辑失败!');
            }
            return back()->with($message);
        }

        $data['box'] = \App\Box::findorfail($request->id);

        return view('admin.box.box_edit', $data);
    }
}
